package controller

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"sport_booking/models"
	"sport_booking/utils"
	"sport_booking/utils/message"
	"sport_booking/utils/validator"
)

/*
参与活动
*/
func AddJoinInfo(c *gin.Context) {
	var join models.Join
	var status int
	err := c.ShouldBindJSON(&join)
	if err != nil {
		message.SendMsg(c, message.ErrorBindData)
		return
	}
	if join.State == false || join.Activity.ID <= 0 {
		message.SendMsg(c, message.ErrorDataFormat)
		return
	}
	// 检查活动ID是否存在
	status = models.CheckActivityIdState(join.Activity.ID)
	if status != message.SuccessActivityIdExist {
		message.SendMsg(c, status)
		return
	}
	// 检查是否重复参与
	join.UUID = c.GetString("uuid")
	status = models.CheckIdCount(&join)
	if status == message.ErrorCheckJoin || status == message.SuccessJoinExist {
		message.SendMsg(c, status)
		return
	}
	join.CreateStamp = utils.GetTimeStamp()
	id, status := models.AddJoinInfo(&join)
	if status != message.SUCCESS {
		message.SendMsg(c, status)
		return
	}
	message.SendMsg(c, message.SUCCESS, id)
}

/*
修改参与活动状态
*/
func UpdateJoinState(c *gin.Context) {
	var join models.Join
	var status int
	err := c.ShouldBindJSON(&join)
	if err != nil {
		message.SendMsg(c, message.ErrorBindData)
		return
	}
	if join.Note == "" || join.ID <= 0 {
		message.SendMsg(c, message.ErrorDataFormat)
		return
	}
	// 检查参与ID是否存在
	status = models.CheckJoinIdState(join.ID)
	if status != message.SuccessJoinIdExist {
		message.SendMsg(c, status)
		return
	}
	join.UUID = c.GetString("uuid")
	join.UpdateStamp = utils.GetTimeStamp()
	status = models.UpdateJoinInfo(&join)
	if status != message.SUCCESS {
		message.SendMsg(c, status)
		return
	}
	message.SendMsg(c, message.SUCCESS)
}

/*
查看活动参与列表
*/
func GetJoinInfoList(c *gin.Context) {
	var next models.Next
	err := c.ShouldBindJSON(&next)
	if err != nil {
		message.SendMsg(c, message.ErrorBindData)
		return
	}
	// 验证
	msg, code := validator.Validate(&next)
	if code != message.SUCCESS {
		c.JSON(http.StatusOK, gin.H{
			"status":  code,
			"message": msg,
		})
		return
	}
	joinInfoList, status := models.GetJoinInfoList(c.GetString("uuid"), &next)
	if status != message.SUCCESS {
		message.SendMsg(c, status)
		return
	}
	message.SendMsg(c, message.SUCCESS, joinInfoList)
}
